package com.frank.towerrush.split;

/**
 * 旗帜类 代表怪物行进路线的点
 * 
 * @author Frank
 *
 */
public class Flag {
    /**
     * x坐标
     */
    private int x;
    /**
     * y坐标
     */
    private int y;

    /**
     * 立个旗
     * 
     * @param x
     *            x坐标值
     * @param y
     *            y坐标值
     */
    public Flag(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    /**
     * 取得x的值
     * 
     * @return x
     */
    public int getX() {
        return x;
    }

    /**
     * 取得y的值
     * 
     * @return y
     */
    public int getY() {
        return y;
    }

    public void move(int dir, int speed) {
        switch (dir) {
        case Road.UP:
            y -= speed;
            break;
        case Road.DOWN:
            y += speed;
            break;
        case Road.RIGHT:
            x += speed;
            break;
        case Road.LEFT:
            x -= speed;
            break;
        default:
            break;
        }
    }

    /**
     * 将位置移动到指定的目标位置
     * 
     * @param there
     *            目标
     */
    public void moveToThere(Flag there) {
        x = there.getX();
        y = there.getY();
    }

    /**
     * 判断是否与目标点重合
     * 
     * @param o
     *            目标
     * @return 结果
     */
    public boolean equals(Flag o) {
        return x == o.getX() && y == o.getY();
    }

    /**
     * 获得一个新的旗帜
     * 
     * @param old
     *            旧的旗帜
     * @return 新的旗帜
     */
    public static Flag newFlag(Flag old) {
        return new Flag(old.getX(), old.getY());
    }

    /**
     * 计算与目标的距离
     * 
     * @param o
     *            目标
     * @return 距离
     */
    public double distance(Flag o) {
        return distance(o.getX() - x, o.getY() - y);
    }

    private double distance(double x, double y) {
        return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    }

    public boolean closeTo(Flag target, int speed) {

        double dx = target.getX() - x;
        double dy = target.getY() - y;
        double dz = distance(dx, dy);
        if (dz <= speed) {
            moveToThere(target);
            return true;
        }
        double sin = dy / dz;
        double cos = dx / dz;

        x += speed * cos;
        y += speed * sin;
        return false;
    }
}
